package com.itheima.news.service.impl;

import com.itheima.news.dao.NewsDao;
import com.itheima.news.pojo.News;
import com.itheima.news.service.NewsService;
import com.itheima.news.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 新闻管理Service实现类
 * @author xiao1.wang@dmall.com
 * @date 2019-07-10 22:32:52
 */
@Service
public class NewsServiceImpl implements NewsService {

    @SuppressWarnings("all")
    @Autowired
    private NewsDao newsDao;

    @Override
    public List<News> list() {
        return newsDao.selectAll();
    }

    @Override
    public void add(News news) {
        Date date = new Date();
        news.setCreateTime(date);
        news.setUpdateTime(date);
        newsDao.insert(news);
    }

    @Override
    public Page<News> findByPage(News news, Integer page, Integer size) {
        Page<News> p = new Page<>();
        p.setPage(page);
        p.setSize(size);
        int start = (page - 1) * size;
        List<News> newsList = newsDao.selectByCondition(news, start, size);
        Integer count = newsDao.selectCount(news);
        p.setCount(count);
        p.setDatas(newsList);
        p.setPageSize((count - 1 + size) / size);
        return p;
    }
}
